package com.wm.exam.service.impl;

import com.wm.exam.dao.ExamVoMapper;
import com.wm.exam.service.ExamVoService;
import com.wm.exam.vo.ExamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ExamVoServiceImpl implements ExamVoService {
    @Autowired
    private ExamVoMapper examVoMapper;

    /**
     * 查询试题
     *
     * @param clazz
     * @return
     */
    @Override
    public List<ExamVo> getByUserClazz(String clazz) {
        return examVoMapper.getByUserClazz(clazz);
    }

    /**
     * 计算分数值
     */
    @Override
    public double getScope(List<ExamVo> list, String answer) {
        // 初始化分数
        double scope = 0.0;
        int firstIndex = answer.indexOf(",");
        String newString = answer.substring(firstIndex + 1);
        String[] strings = newString.split(",");
        // 初始化用户提交的答案
        List answers = new ArrayList();
        for (String s : strings) {
            answers.add(s);
        }
        // 先取出来正确的答案
        List trueList = new ArrayList();
        for (ExamVo trueAnswer : list) {
            trueList.add(trueAnswer.getTrueAnswer());

        }
        // 计算每一题的分数值
        double signAnswer = list.get(0).getScopeValue() / list.size();
        // 和填写的答案进行比较
        for (int i = 0; i < list.size(); i++) {
            if (answers.get(i).equals(trueList.get(i))) {
                scope = scope + signAnswer;
            }
        }

        return scope;
    }


}
